package com.example.yls.demoa;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSimpleAPPDialog1();
            }
        });
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSimpleAPPDialog2();
            }
        });
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSimpleAPPDialog3();
            }
        });
        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSimpleAPPDialog4();
            }
        });
        findViewById(R.id.btn5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMultiChoice();
            }
        });
        findViewById(R.id.btn6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showWaitingDiaglog6();
            }
        });
        findViewById(R.id.btn7).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showProgressDialog7();
            }
        });
        findViewById(R.id.btn8).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEditDialog8();
            }
        });
    }

    private void showEditDialog8() {
       final EditText editText = new EditText(MainActivity.this);
        AlertDialog.Builder inputDialog = new AlertDialog.Builder(MainActivity.this);
        inputDialog.setView(editText)
                .setTitle("这是一个编辑Dialog,你可以在下方输入文字")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"你输入的是:"+editText.getText().toString(),Toast.LENGTH_SHORT).show();
                    }
                }).create().show();
    }

    private void showProgressDialog7() {
        final int PROGRESS_MAX=100;//设置进度条的最大值
        final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setProgress(0);
        progressDialog.setTitle("我是一个孤独的进度条");
        progressDialog.setMax(PROGRESS_MAX);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();
        //模拟一个线程，每隔0.1增加一个进度
        new Thread(new Runnable() {
            @Override
            public void run() {
                int progress = 0;
                while (progress<PROGRESS_MAX){
                    try {
                        Thread.sleep(100);
                        progress++;
                        progressDialog.setProgress(progress);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //当进度条到达最大时，自动关闭
                progressDialog.cancel();
            }

        }).start();
    }

    private void showWaitingDiaglog6() {
        ProgressDialog waitingDialog=
        new ProgressDialog(MainActivity.this);
        waitingDialog.setTitle("我是一个等待Dialog");
        waitingDialog.setMessage("等待中...");
        waitingDialog.setIndeterminate(true);
        waitingDialog.setCancelable(true);
        waitingDialog.show();
        waitingDialog.setCanceledOnTouchOutside(true);
    }

    ArrayList<Integer> yourChoices = new ArrayList<>();
    private void showMultiChoice() {
        final String[] hoblies={"篮球","足球","网球","LOL"};
        final boolean initChoiceSets[]= new boolean[hoblies.length];
        for(int i=0;i<initChoiceSets.length;i++){
            initChoiceSets[i]=false;
        }

        AlertDialog.Builder multiChoiceDialog = new AlertDialog.Builder(MainActivity.this);
        multiChoiceDialog.setTitle("这是一个多选框，请选择多个你的爱好");
        multiChoiceDialog.setMultiChoiceItems(hoblies, initChoiceSets, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if (isChecked){
                    yourChoices.add(which);
                }else{
                    yourChoices.remove(which);
                }
            }
        });


                multiChoiceDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int size = yourChoices.size();
                        String str = "";
                        for (int i = 0; i < size; i++) {
                            str += hoblies[yourChoices.get(i)] + "  ";
                        }
                        Toast.makeText(MainActivity.this, "选中了" + str, Toast.LENGTH_SHORT).show();

                    }
                });
                multiChoiceDialog.create().show();
    }

    int youchoice=-1;
    private void showSimpleAPPDialog4() {

        final String[] hobbies = {"篮球","足球","网球","LOL"};
        AlertDialog.Builder singleChoiceDialog = new AlertDialog.Builder(MainActivity.this);
        singleChoiceDialog.setTitle("这是一个单选框，请选择一个你的爱好鸭！");
        singleChoiceDialog.setSingleChoiceItems(hobbies, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                youchoice=which;
            }
        });
        singleChoiceDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (youchoice!=-1){
                    Toast.makeText(MainActivity.this,
                            "你选择了" + hobbies[youchoice],
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        singleChoiceDialog.create().show();




    }

    private void showSimpleAPPDialog3() {
        final String[] sex = {"男","女","人妖"};
        final String[] cities = {"广州","上海","深圳","北京","香港","澳门"};
        new AlertDialog.Builder(MainActivity.this)
                .setTitle("请选择一个城市")
                .setIcon(R.mipmap.ic_launcher)
                /*.setSingleChoiceItems(sex, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"您的性别是：  "+sex[i],Toast.LENGTH_SHORT).show();
                        dialogInterface.cancel();
                    }
                })*/

                .setItems(cities, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"选择城市为：  "+cities[i],Toast.LENGTH_SHORT).show();
                    }
                }).show();


    }

    private void showSimpleAPPDialog2() {
        new AlertDialog.Builder(MainActivity.this)
                .setTitle("弹出警告框")
                .setIcon(R.mipmap.ic_launcher)
                .setMessage("你确定要删除吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"positive:  "+i,Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"negative:  "+i,Toast.LENGTH_SHORT).show();
                    }
                })
                .setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"neutral:  "+i,Toast.LENGTH_SHORT).show();

                    }
                }).show();
    }

    private void showSimpleAPPDialog1() {
        new AlertDialog.Builder(MainActivity.this)
                .setTitle("这是标题")
                .setIcon(R.mipmap.ic_launcher)
                .setMessage("这是消息")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .create()
                .show();
    }
}
